<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * Class ArticleController
 * @package Admin\Controller
 * 2017-12-13   文章类
 */
class ArticleController extends AdminBasicController{

    public $article = '';
    public function _initialize(){
        $this->checkLogin();
        $this->article = D('Article');
    }

    /**
     * 文章列表
     * @author mss
     * @time 2017-12-13
     */
    public function articleList(){
        $shop_id = I('get.shop_id');
        $where = array();
        $param = array();
        $where['status'] = 0;
        $order = 'ctime DESC';
        /**按文章标题搜索*/
        if(!empty(trim(I('request.title')))){
            $title = trim(I('request.title'));
            $where['title'] = array('LIKE','%'.$title.'%');
            $param['title'] = $title;
        }
        /**按商家名称或账号搜索*/
        if(!empty(trim(I('request.name')))){
            $name = trim(I('request.name'));
            $w['name|account'] = array('LIKE','%'.$name.'%');
            $w['status'] = array('NEQ',9);
            $ids = M('Shop')->where($w)->order('ctime DESC')->getField('shop_id',true);
            $where['shop_id'] = array('IN',$ids);
            $param['name'] = $name;
        }
        /**按发布时间查找*/
        if(!empty(I('request.start_time')) && !empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $param['start_time'] = I('request.start_time');
            $param['end_time'] = I('request.end_time');
        }

        /**从商家列表跳转过来*/
        if(!empty($shop_id)){
            $where['shop_id'] = $shop_id;
            $param['shop_id'] = $shop_id;
        }

        $list = $this->article->selectArticle($where,$order,15,$param);
        $data = $list['list'];
        foreach($data as $k=>$v){
            $data[$k]['shop_name'] = M('Shop')->where(array('shop_id'=>$v['shop_id']))->getField('name');
        }

        $this->assign('list',$data);
        $this->assign('page',$list['page']);
        $this->assign('request',$_REQUEST);

        $this->display('articleList');
    }

    /**
     * 文章详情
     * @author mss
     * @time 2017-12-14
     */
    public function articleDetail(){
        $id = $_GET['art_id'];
        $info = M('Article')->where(array('art_id'=>$id))->find();
        $this->assign('info',$info);
        $shop_name = M('Shop')->where(array('shop_id'=>$info['shop_id']))->getField('name');
        $this->assign('shop_name',$shop_name);

        $this->display('articleDetail');
    }

    /**
     * 添加文章
     * @author mss
     * @time 2017-11-24
     */
    public function addArticle(){
        if(IS_POST){
            $data = $this->article->create();
            if($data){
                $res = $this->article->addArticle($data);
                if($res){
                    $this->success('添加成功',U('Article/articleList'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($this->article->getError());
            }
        }else{

            $this->display('addArticle');
        }

    }

    /**
     * 编辑文章
     * @author mss
     * @time 2017-11-24
     * @param art_id 文章id
     */
    public function editArticle(){
        if(IS_POST){
            $data = $this->article->create();
            if($data){
                $where['art_id'] = $_POST['art_id'];
                $data['utime'] = time();
                $res = $this->article->editArticle($where,$data);
                if($res){
                    $this->success('编辑成功',U('Article/articleList'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($this->article->getError());
            }
        }else{
            $id = I('art_id');
            $info = M('Article')->where(array('art_id'=>$id))->find();
            $this->assign('info',$info);
            $this->display('editArticle');
        }
    }

    /**
     * 删除文章
     * @author mss
     * @time 2017-11-24
     */
    public function delArticle(){
        if(empty($_REQUEST['art_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['art_id'] = array('IN',I('request.art_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->article->editArticle($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

    /**
     * 修改文章显示顺序
     */
    public function ajaxUpArticle(){
        $art_id = $_POST['art_id'];
        $data['sort'] = $_POST['sort'];
        $data['utime'] = time();
        $where['art_id'] = $art_id;
        $res = M('Article')->where($where)->save($data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

}